//
//  ByUIStoreIndexVC.m
//  chequanbao_ios
//
//  Created by pro on 17/5/25.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIStoreIndexVC.h"

// cell
#import "ByCellStoreIndex.h"

// api
#import "ByApiStore.h"

@interface ByUIStoreIndexVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation ByUIStoreIndexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商城";
    [self addLeftBackBarButtonItem];
    
    // 模拟数据
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
    
    [self createTableView];
    
    [self getStoreList];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
}


#pragma mark - 请求数据
- (void)getStoreList {
    
    [ByApiStore getStoreList:[ByNetParamsModel new] success:^(ByNetBaseResult *result) {
        
        [self.items addObjectsFromArray:result.data];
        [self.table reloadData];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}

#pragma mark - 懒加载
- (NSMutableArray *)items {
    
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}

#pragma mark - 创建视图
// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(10, 64, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor clearColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    self.table.tableFooterView = [UIView new];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.items.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellStoreIndex *cell = [tableView dequeueReusableCellWithIdentifier:@"ByCellStoreIndex"];
    if (cell == nil) {
        cell = [[ByCellStoreIndex alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ByCellStoreIndex"];
    }
    cell.dataDic = self.items[indexPath.section];
    
    cell.tapBlock = ^(NSString *link, NSString *name) {
        
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:link];
        vc.titleString = name;
        [self by_normalPush:vc hasNavi:YES];
    };
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}


@end
